package com.unittec.zk.provider.flowable.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unittec.zk.provider.flowable.service.IFlowInstanceService;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.utils.ValidationUtils;
import com.unittec.zk.sdk.flowable.domain.FlowProcDefDto;
import com.unittec.zk.sdk.flowable.request.*;
import com.unittec.zk.sdk.flowable.response.*;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.BizCode;
import com.unittec.zk.sdk.root.enums.BizCodeEnum;
import com.unittec.zk.sdk.root.enums.ErrorEnum;
import com.unittec.zk.sdk.root.internal.exception.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "工作流流程实例管理")
@RestController
public class FlowInstanceController {
    @Autowired
    private IFlowInstanceService flowInstanceService;

    @Autowired
    private Log log;


    @RequestMapping(value = "/666021", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据流程定义id启动流程实例", notes = "根据流程定义id启动流程实例")
    public Result<Res666021> startById(@RequestBody GatewayRequest<Req666021> req){
        log.debug(req, "startById  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res666021 res666021 = null;
        try {
            Req666021 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res666021 = new Res666021();
            String info = flowInstanceService.startProcessInstanceById(content.getProcDefId(), content.getVariables());
            res666021.setContent(info);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "startById {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res666021);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/666022", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "激活或挂起流程实例", notes = "激活或挂起流程实例")
    public Result<Res666022> updateState(@RequestBody GatewayRequest<Req666022> req){
        log.debug(req, "updateState  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res666022 res666022 = null;
        try {
            Req666022 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res666022 = new Res666022();
            flowInstanceService.updateState(content.getState(),content.getInstanceId());
            res666022.setContent("激活或挂机流程实例成功");
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "updateState {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res666022);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/666023", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "结束流程实例", notes = "结束流程实例")
    public Result<Res666023> stopProcessInstance(@RequestBody GatewayRequest<Req666023> req){
        log.debug(req, "stopProcessInstance  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res666023 res666023 = null;
        try {
            Req666023 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res666023 = new Res666023();
            flowInstanceService.stopProcessInstance(content.getFlowTaskVo());
            res666023.setContent("结束流程实例成功");
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "stopProcessInstance {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res666023);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

    @RequestMapping(value = "/666024", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除流程实例", notes = "删除流程实例")
    public Result<Res666024> delete(@RequestBody GatewayRequest<Req666024> req){
        log.debug(req, "delete  {} ,param {}", "begin", JSON.toJSONString(req.getContent()));
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res666024 res666024 = null;
        try {
            Req666024 content = req.getContent();
            ValidationUtils.validateEntityStr(content);
            res666024 = new Res666024();
            flowInstanceService.delete(content.getInstanceId(),content.getDeleteReason());
            res666024.setContent("删除流程实例成功");
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            if (e instanceof BusinessException) {
                return Result.error(req, e.getMessage());
            } else {
                return Result.error(req, ErrorEnum.QUERY_ERROR.getErrorMessage());
            }
        }
        log.debug(req, "delete {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req, res666024);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }


}
